package com.cmas.platform.service.impl;

import java.util.List;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cmas.platform.vo.ApproveConfigVo;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmas.platform.mapper.ApproveConfigMapper;
import com.cmas.platform.domain.ApproveConfig;
import com.cmas.platform.service.ApproveConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 审核配置表Service业务层处理
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-13
 */
@Service
public class ApproveConfigServiceImpl extends ServiceImpl<ApproveConfigMapper, ApproveConfig> implements ApproveConfigService {

    private static final Logger log = LoggerFactory.getLogger(ApproveConfigServiceImpl.class);


    /**
     * 查询审核配置表列表
     *
     * @param approveConfig 审核配置表
     * @return 审核配置表
     */
    @Override
    public List<ApproveConfig> selectApproveConfigList(ApproveConfig approveConfig) {
        QueryWrapper<ApproveConfig> queryWrapper = setQueryWrapper(approveConfig);
        return baseMapper.selectList(queryWrapper);
    }


    @Override
    public List<ApproveConfigVo> selectApproveConfigVoList(ApproveConfig approveConfig) {
        QueryWrapper<ApproveConfig> queryWrapper = setQueryWrapper(approveConfig);
        return baseMapper.selectApproveConfigVoList(queryWrapper);
    }


    /**
     * 设置QueryWrapper条件
     *
     * @param
     * @return
     */
    public QueryWrapper<ApproveConfig> setQueryWrapper(ApproveConfig approveConfig) {
        QueryWrapper<ApproveConfig> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(approveConfig.getName())) {
            queryWrapper.like("cmas.approve_config.name", approveConfig.getName());
        }
        if (StringUtils.isNotEmpty(approveConfig.getItemId())) {
            queryWrapper.eq("cmas.approve_config.item_id", approveConfig.getItemId());
        }
        if (StringUtils.isNotEmpty(approveConfig.getMethod())) {
            queryWrapper.eq("cmas.approve_config.method", approveConfig.getMethod());
        }
        if (StringUtils.isNotEmpty(approveConfig.getStatus())) {
            queryWrapper.eq("cmas.approve_config.status", approveConfig.getStatus());
        }
        return queryWrapper;
    }

    /**
     * 批量删除审核配置表
     *
     * @param ids 需要删除的审核配置表ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteApproveConfigByIds(String[] ids) {
        return baseMapper.delete(new QueryWrapper<ApproveConfig>().lambda()
                .in(ApproveConfig::getId, ids).eq(ApproveConfig::getStatus, "1"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addApproveConfig(ApproveConfig approveConfig) {
        if (approveConfig.getStatus().equals("0")) {
            baseMapper.update(null, new UpdateWrapper<ApproveConfig>().lambda()
                    .eq(ApproveConfig::getItemId, approveConfig.getItemId())
                    .eq(ApproveConfig::getStatus, "0")
                    .set(ApproveConfig::getStatus, "1"));
        }
        baseMapper.insert(approveConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateApproveConfig(ApproveConfig approveConfig) {
        if (approveConfig.getStatus().equals("0")) {
            baseMapper.update(null, new UpdateWrapper<ApproveConfig>().lambda()
                    .eq(ApproveConfig::getItemId, approveConfig.getItemId())
                    .eq(ApproveConfig::getStatus, "0")
                    .set(ApproveConfig::getStatus, "1"));
        } else {
            ApproveConfig oldApproveConfig = baseMapper.selectOne(new QueryWrapper<ApproveConfig>().lambda()
                    .eq(ApproveConfig::getItemId, approveConfig.getItemId())
                    .eq(ApproveConfig::getStatus, "0"));
            if (oldApproveConfig.getId().equals(approveConfig.getId())) {
                return false;
            }
        }
        baseMapper.updateById(approveConfig);
        return true;
    }

}
